#!/bin/sh
# Test script for minapl.
# Copyright 2011 Michael A. Muller
# Options:
#   -v use valgrind
#   -H use valgrind with full leak-check output.

args=$(getopt 'vH' "$@")
eval set -- "$args"

check_output=true
wrapper=
while true; do
    echo "1 is $1, 2 is $2"
    case "$1" in
        -v) wrapper=valgrind; check_output=false ;;
        -H) wrapper="valgrind --leak-check=full"; check_output=false ;;
        --) shift; echo wtf; break ;;
    esac
    echo shifting
    shift
done

test_dir=$(dirname $0)
minapl="$wrapper $test_dir/../minapl"

got_failures=false

runtest() {
    filename=$1
    outname=${1%%.m}.out
    /bin/echo -e -n "\033[37mrunning test $filename..."
    if $check_output; then
        if ! $minapl $filename 2>&1 | diff $outname -; then
            /bin/echo -e "  \033[31mFAIL"
            got_failures=true
        else
            /bin/echo -e ' \033[32mPASS'
        fi
    else
        $minapl $filename
    fi
}

/bin/echo -e "\033[33;40mRunning tests..."
for file in $test_dir/*.m; do
    runtest $file
done

if $got_failures; then
    /bin/echo -e "\033[31mSome tests failed"
fi

